package com.shushan.pages;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.android.net.bean.NetRetBean;
import com.android.net.core.common.RequestType;
import com.android.net.helper.NetHelper;
import com.android.net.listener.common.CallbackCode;
import com.android.net.url.UrlParse;
import com.shushan.base.Constants;
import com.shushan.base.NavigationLoseActivity;
import com.shushan.db.ChildBeanDao;
import com.shushan.home.adapter.StarMallRecyclerAdapter;
import com.shushan.home.bean.StarsDataBean;
import com.shushan.login.bean.ChildBean;
import com.shushan.login.bean.UserBean;
import com.shushan.network.UrlCommParse;
import com.shushan.network.UrlConst;
import com.shushan.network.listener.NetSingleCommListener;
import com.shushan.pages.bean.MyStarsDataBean;
import com.shushan.shushanbao.BabyApplication;
import com.shushan.shushanbao.R;
import com.shushan.ui.GridRecyclerDivider;
import com.shushan.util.DensityUtil;
import com.shushan.util.SharedPreferencesUtils;
import com.shushan.util.ToolUtils;
import com.shushan.web.GoodsDetailWebActivity;

import java.util.List;

/**
 * @ClassName: StarMallActivity
 * @Desciption: //学贝商城界面
 * @author: zhangshihao
 * @date: 2018-09-21
 */
public class StarMallActivity extends NavigationLoseActivity implements View.OnClickListener{

    private static final String TAG = StarMallActivity.class.getSimpleName();

    private ImageView ivGetNow;
    private RecyclerView rvStarMall;
    private RelativeLayout rlFirstToy;

    private List<StarsDataBean> starsDataBeanList;
    private BabyApplication babyApplication = BabyApplication.getInstance();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_star_mall);

        setTitle("学贝商城");

        initView();

        getData();
    }

    private void initView(){
        ivGetNow = findViewById(R.id.iv_get_now);
        ivGetNow.setOnClickListener(this);
        rvStarMall = findViewById(R.id.rv_star_mall);
        rvStarMall.addItemDecoration(new GridRecyclerDivider(2, DensityUtil.dip2px(this,10)));
        rvStarMall.setLayoutManager(new GridLayoutManager(this,2));
        rlFirstToy = findViewById(R.id.rl_first_toy);
    }

    private void getData(){
        int childId = babyApplication.getCurrentChildId();
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.MY_STARS);
        UrlParse paramParse = new UrlCommParse(this)
                .putValue("child_id",childId+"");

        showWaitDialog();
        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(paramParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<MyStarsDataBean>() {
                    @Override
                    protected void onCommon() {
                        dismissWaitDialog();
                    }

                    @Override
                    protected void onSuccess(MyStarsDataBean myStarsDataBean) {

                        setData(myStarsDataBean);

                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {

                    }
                });

    }

    private void setData(MyStarsDataBean myStarsDataBean){
        if(myStarsDataBean == null){
            return;
        }
        if(myStarsDataBean.starsChildBean != null){
            if(!TextUtils.isEmpty(myStarsDataBean.starsChildBean.unclaimed_prize)){
                rlFirstToy.setVisibility(View.GONE);
            }else{
                rlFirstToy.setVisibility(View.VISIBLE);
            }
        }
        starsDataBeanList = myStarsDataBean.getStarsDataBeanList();
        if(!ToolUtils.isListEmpty(starsDataBeanList)) {
            StarMallRecyclerAdapter smRecyclerAdapter = new StarMallRecyclerAdapter(this, starsDataBeanList);
            smRecyclerAdapter.setOnItemClickListener(new StarMallRecyclerAdapter.OnItemClickListener() {
                @Override
                public void onItemClick(int position) {
                    //跳转H5商品详情页
                    gotoGoodsWebDetail(starsDataBeanList.get(position));
                }
            });
            rvStarMall.setAdapter(smRecyclerAdapter);
        }else{
            Log.d(TAG,"starsDataBeanList is empty");
        }
    }

    private void gotoGoodsWebDetail(StarsDataBean starsDataBean) {
        if(starsDataBean == null){
            return;
        }

        ChildBean currentChildBean = babyApplication.getCurrentChildBean();
        UserBean currentUserBean = babyApplication.getUserBean();
        int childId = currentChildBean.id;
        UrlParse urlParse = new UrlParse(/*UrlConst.BASE_URL*/"https://tpi.shushan.com")
                .appendRegion(UrlConst.DIST_TOY_HTML)
                .putValue("phone", SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY))
                .putValue("childid", (childId > 0) ? (childId+"") : "")
                .putValue("starsnum",currentChildBean.stars)
                .putValue("goodsid",starsDataBean.id)
                .putValue("viptype",ToolUtils.getVipTypeForGoodsDetails(currentUserBean))
                ;

        Intent intent = new Intent(this, GoodsDetailWebActivity.class);
        intent.putExtra("title","奖品详情");
        intent.putExtra("url",urlParse.toString());
        startActivityForResultAllow(intent,100);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.iv_get_now://立即领取
                //跳转奖品展示网页
                gotoGoodsWebDetail();
                break;
        }
    }

    private void gotoGoodsWebDetail() {
        ChildBean currentChildBean = babyApplication.getCurrentChildBean();
        UserBean currentUserBean = babyApplication.getUserBean();
        int childId = currentChildBean.id;
        UrlParse urlParse = new UrlParse(/*UrlConst.BASE_URL*/"https://tpi.shushan.com")
                .appendRegion(UrlConst.DIST_TOY_HTML)
                .putValue("phone", SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY))
                .putValue("childid", (childId > 0) ? (childId+"") : "")
                .putValue("starsnum",currentChildBean.stars)
                .putValue("goodsid",0)
                .putValue("viptype", ToolUtils.getVipTypeForGoodsDetails(currentUserBean))
                ;

        Intent intent = new Intent(this, GoodsDetailWebActivity.class);
        intent.putExtra("title","奖品详情");
        intent.putExtra("url",urlParse.toString());
        startActivityForResultAllow(intent,200);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 200){
            if(requestCode == 200){//领取first_toy
                showToast("领取成功");
                rlFirstToy.setVisibility(View.GONE);
            }else {
                showToast("兑换奖品成功");
                getUserinfo();
            }
        }else if(resultCode == 300){
            onBackAction();
        }else if(resultCode == 500){//玩具详情点击购买
            startActivity(OpenVipSpendActivity.class);
        }
    }

    public void getUserinfo() {
        UrlParse urlParse = new UrlCommParse(this, UrlConst.BASE_URL).appendRegion(UrlConst.GET_USER_INFO);
        urlParse.putValue("phone",SharedPreferencesUtils.getStringDate(Constants.LOGIN_MOBILE_SHAREPREFERS_KEY));

        NetHelper.create()
                .url(urlParse.toStringOnlyHeader())
                .param(urlParse.toStringOnlyParam())
                .request(RequestType.REQUEST_TYPE_POST, new NetSingleCommListener<UserBean>() {

                    @Override
                    protected void onSuccess(UserBean userBean) {
                        if(userBean != null){
                            babyApplication.setUserBean(userBean);

                            if(!TextUtils.isEmpty(userBean.password)){
                                SharedPreferencesUtils.setStringDate(Constants.PARENT_PASSWD_SHAREPREFERS_KEY,userBean.password);
                            }

                            List<ChildBean> childList = userBean.getChildList();
                            if(childList.size() > 0){
                                new ChildBeanDao(babyApplication).dropAndAddList(childList);
                            }else {
                                new ChildBeanDao(babyApplication.getInstance()).clearTab();
                            }
                        }
                    }

                    @Override
                    protected void onError(CallbackCode errorCode, NetRetBean netRetBean) {
                        showNoMsgError(netRetBean);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        setResult(400);
    }
}
